package app;

import app.database.MultimediaDBItem;
import app.models.MultimediaModel;
import java.awt.Image;
import java.io.File;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFileChooser;

/**
 * Dialog zobrazujicí detail objektu z multimediální databáze, používá se i na přidávání
 * @author Lukáš Marek
 */
public class DetailWindow extends javax.swing.JDialog {
	private final MultimediaModel model;
	private int id = -1;
	private String newImagePath = "";
	
	/** 
	 * Konstruktor, vytvori nove okno
	 * @param model model
	 * @param id id objektu
	 */
	public DetailWindow(MultimediaModel model, int id)
	{
		super((JDialog)null, true);
		initComponents();
		this.setLocationRelativeTo(null);
		this.model = model;
		LoadObjectInfo(id);
		this.id = id;
	}

 /**
	* Naplni gui informacemi o objektu
	* @param id 
	*/        
	private void LoadObjectInfo(int id)
	{
		if (id == -1) return;
		MultimediaDBItem item = (MultimediaDBItem) model.loadObject(id);
		if(item.image != null)
		{
			Image im;
			int width = jLabel1.getWidth();
			int height = jLabel1.getHeight();
			if ((item.image.getIconHeight() / (double)item.image.getIconWidth()) < (height / (double)width))
				im = item.image.getImage().getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH);
			else
				im = item.image.getImage().getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH);
			jLabel1.setIcon(new ImageIcon(im));
		}

		NameField.setText(item.name);
		LvlField.setText(item.lvl);
		DescArea.setText(item.description);
		if(!"".equals(item.name)) setTitle(item.name + " - podrobnosti");
	}
                
	/** This method is called from within the constructor to
	 * initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is
	 * always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents()
  {

    NameField = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jLabel5 = new javax.swing.JLabel();
    jLabel9 = new javax.swing.JLabel();
    jScrollPane1 = new javax.swing.JScrollPane();
    DescArea = new javax.swing.JTextArea();
    SaveButton = new javax.swing.JButton();
    LvlField = new javax.swing.JTextField();
    loadNewImageButton = new javax.swing.JButton();
    jLabel1 = new javax.swing.JLabel();
    jLabelStatus = new javax.swing.JLabel();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle("Nová nestvůra");
    setMinimumSize(new java.awt.Dimension(730, 470));
    setPreferredSize(new java.awt.Dimension(730, 470));
    setResizable(false);
    getContentPane().setLayout(null);

    NameField.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    getContentPane().add(NameField);
    NameField.setBounds(120, 20, 255, 22);

    jLabel3.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    jLabel3.setText("Název objektu:");
    getContentPane().add(jLabel3);
    jLabel3.setBounds(10, 20, 96, 20);

    jLabel5.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    jLabel5.setText("Level:");
    getContentPane().add(jLabel5);
    jLabel5.setBounds(520, 20, 38, 20);

    jLabel9.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    jLabel9.setText("Popis:");
    getContentPane().add(jLabel9);
    jLabel9.setBounds(390, 70, 39, 16);

    DescArea.setColumns(20);
    DescArea.setLineWrap(true);
    DescArea.setRows(5);
    DescArea.setWrapStyleWord(true);
    DescArea.setMaximumSize(new java.awt.Dimension(104, 64));
    jScrollPane1.setViewportView(DescArea);

    getContentPane().add(jScrollPane1);
    jScrollPane1.setBounds(390, 90, 310, 190);

    SaveButton.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    SaveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/symbol_change.png"))); // NOI18N
    SaveButton.setText("Uložit");
    SaveButton.setToolTipText("uloží změny do databáze");
    SaveButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        SaveButtonActionPerformed(evt);
      }
    });
    getContentPane().add(SaveButton);
    SaveButton.setBounds(530, 360, 160, 33);

    LvlField.setFont(new java.awt.Font("Verdana", 1, 12)); // NOI18N
    getContentPane().add(LvlField);
    LvlField.setBounds(580, 20, 120, 22);

    loadNewImageButton.setFont(new java.awt.Font("Verdana", 0, 12)); // NOI18N
    loadNewImageButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/data/symbol_open.png"))); // NOI18N
    loadNewImageButton.setText("Nahrát obrázek");
    loadNewImageButton.setToolTipText("přiřadí nestvůře ilustrační obrázek");
    loadNewImageButton.addActionListener(new java.awt.event.ActionListener()
    {
      public void actionPerformed(java.awt.event.ActionEvent evt)
      {
        loadNewImageButtonActionPerformed(evt);
      }
    });
    getContentPane().add(loadNewImageButton);
    loadNewImageButton.setBounds(10, 400, 160, 33);

    jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
    jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    getContentPane().add(jLabel1);
    jLabel1.setBounds(10, 60, 368, 322);
    getContentPane().add(jLabelStatus);
    jLabelStatus.setBounds(540, 320, 140, 20);

    pack();
  }// </editor-fold>//GEN-END:initComponents

/**
	* Zapis zmen do databaze
	* @param evt 
	*/
private void SaveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SaveButtonActionPerformed
	MultimediaDBItem item = new MultimediaDBItem();
	item.id = id;
	item.name = NameField.getText();
	item.lvl = LvlField.getText();
	item.description = DescArea.getText();
	
	if(!newImagePath.equals(""))
	{
		item.image = new ImageIcon(newImagePath);
		newImagePath = "";
	}
	
	try
	{
		model.saveObject(item);
		jLabelStatus.setText("Uloženo");
	}
	catch (Exception ex)
	{
		System.err.println("Nepodarilo se ulozit objekt");
		System.err.println(ex.getMessage());
		jLabelStatus.setText("Chyba");
	}
}//GEN-LAST:event_SaveButtonActionPerformed

/**
 * nacteni noveho obrazku
 * @param evt 
 */
private void loadNewImageButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadNewImageButtonActionPerformed
	JFileChooser fc = new JFileChooser();
	int returnVal = fc.showOpenDialog(this);
	if (returnVal == JFileChooser.APPROVE_OPTION)
	{
		File file = fc.getSelectedFile();
		ImageIcon icon = new ImageIcon(file.getPath());
		Image im;
		int width = jLabel1.getWidth();
		int height = jLabel1.getHeight();
		if ((icon.getIconHeight() / (double)icon.getIconWidth()) < (height / (double)width))
			im = icon.getImage().getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH);
		else
			im = icon.getImage().getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH);
		jLabel1.setIcon(new ImageIcon(im));
		newImagePath = file.getPath();
	}
}//GEN-LAST:event_loadNewImageButtonActionPerformed
	
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JTextArea DescArea;
  private javax.swing.JTextField LvlField;
  private javax.swing.JTextField NameField;
  public javax.swing.JButton SaveButton;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel9;
  private javax.swing.JLabel jLabelStatus;
  private javax.swing.JScrollPane jScrollPane1;
  private javax.swing.JButton loadNewImageButton;
  // End of variables declaration//GEN-END:variables
}
